local hq = class("headquarter",Base)
hq.aabb = true
hq.tag = "hq"
hq.life = 1

hq.core = {{0,0},{0,1},
		   {1,0},{1,1}}
hq.inner = {
	{-1,-1},{0,-1},{1,-1},{2,-1},
	{-1,0},               {2, 0},
	{-1,1},               {2, 1},
	{-1,2}, {0,2}, {1,2}, {2, 2}	
}

hq.outer = {
	{-2,-2},{-1,-2},{0,-2},{1,-2},{2,-2},{3,-2},
	{-2,-1},               		  		 {3,-1},
	{-2, 0},               		  	 	 {3, 0},
	{-2, 1},					  		 {3, 1},
	{-2, 2},					  		 {3, 2},
	{-2, 3},{-1, 3},{0, 3},{1, 3},{2, 3},{3, 3},
}

function hq:init(game,ix,iy,team)
	self.game = game
    self.ix = ix
	self.iy = iy
	self.team = team
	for i,tile in ipairs(self.core) do
		self:tryTile(tile[1]+ix,tile[2]+iy,6)
	end

	for i,tile in ipairs(self.inner) do
		self:tryTile(tile[1]+ix,tile[2]+iy,2)
	end

	for i,tile in ipairs(self.outer) do
		self:tryTile(tile[1]+ix,tile[2]+iy,2)
	end
	self.scale= self.game.map.size/8
	self.x = (self.ix-1)*self.scale*8
	self.y = (self.iy-1)*self.scale*8
	self.game.world:add(self,self.x,self.y,self.scale*16,self.scale*16)

	self:createData(ix,iy,team)
end


function hq:tryTile(x,y,t)
	if self.game.map.tiles[x] and self.game.map.tiles[x][y] then
		self.game.map:setTile(x,y,t)
	end
end

function hq:update()

end

function hq:damage()
    self.life = self.life - 1
    if self.life < 1 and not self.fallen then
        self.fallen = true
        if SERVER then
        	net:sendToRoom(self.game.room,"hq_destroyed",{id = self.id})
        else
        	sound:play("destroyme")
        end
    end
end

function hq:build()
	for i,tile in ipairs(self.inner) do
		self:tryTile(tile[1]+self.ix,tile[2]+self.iy,3)
	end

	for i,tile in ipairs(self.outer) do
		self:tryTile(tile[1]+self.ix,tile[2]+self.iy,6)
	end
end

function hq:unbuild()
	for i,tile in ipairs(self.inner) do
		self:tryTile(tile[1]+self.ix,tile[2]+self.iy,6)
	end

	for i,tile in ipairs(self.outer) do
		self:tryTile(tile[1]+self.ix,tile[2]+self.iy,6)
	end
end

function hq:normal()
	for i,tile in ipairs(self.inner) do
		self:tryTile(tile[1]+self.ix,tile[2]+self.iy,2)
	end

	for i,tile in ipairs(self.outer) do
		self:tryTile(tile[1]+self.ix,tile[2]+self.iy,2)
	end
end


function hq:draw()
	love.graphics.setColor(1, 1, 1, 1)
	if self.fallen then
		texture:draw("fallen",self.x,self.y,0,self.scale,self.scale)
	else
		texture:draw("hq",self.x,self.y,0,self.scale,self.scale)
	end
end


return hq